package com.c2soft.ncmxTrade.controller.page;

import java.net.URI;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UriComponentsBuilder;

import com.c2soft.ncmxTrade.command.BrokerFormCommand;
import com.c2soft.ncmxTrade.command.OrderCommand;
import com.c2soft.ncmxTrade.util.JsonUtil;


@Log4j2
@Controller
public class CommonController {
	
	@Value("#{systemProperties['ncmxServer.redirect.url']}")
	private String baseServerUrl;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value = "/tradeBook",method=RequestMethod.GET)
	
	public @ResponseBody List tradeBook(HttpServletRequest req) {
		
		String userId=(String) req.getSession().getAttribute("trdUsrId");
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("tradeBook")
			    .queryParam("userId", userId)
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}
	
@RequestMapping(value = "/userAccountDetail",method=RequestMethod.GET)
	
	public @ResponseBody List accountDetail(HttpServletRequest req) {
		String userId=(String) req.getSession().getAttribute("trdUsrId");
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("user/userAccountDetail")
			    .queryParam("userId", userId)
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};
		return response;
	}
	

@RequestMapping(value = "/countProfitLoss",method=RequestMethod.GET)

public @ResponseBody List countProfitLoss(HttpServletRequest req) {
	
	String userId=(String) req.getSession().getAttribute("trdUsrId");
	
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("ProfitLoss")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	String[] appendArray={"{\"rows\":",response.toString(),"}"};
	return response;
}



@RequestMapping(value = "/netPosition",method={RequestMethod.GET,RequestMethod.POST})
	
	public @ResponseBody List netPosition(HttpServletRequest req,@RequestBody String json) {
		
		String userId=(String) req.getSession().getAttribute("trdUsrId");
		OrderCommand order= (OrderCommand) JsonUtil.fromJson(json,OrderCommand.class);
		order.setUserId(userId);
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("netPosition")
			    .queryParam("userId",userId)
			    .queryParam("ptype", order.getPtype())
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};
		return response;
	}

@RequestMapping(value = "/prevtradeBook",method=RequestMethod.GET)

public @ResponseBody List prevtradeBook(HttpServletRequest req) {
	
	String userId=(String) req.getSession().getAttribute("trdUsrId");
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("prevtradeBook")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	String[] appendArray={"{\"rows\":",response.toString(),"}"};
	return response;
}

@RequestMapping(value = "/rmsNews",method={RequestMethod.POST,RequestMethod.GET},headers = "Accept=application/json")

public @ResponseBody List rmsNews(HttpServletRequest req) {
	
	String userId=(String) req.getSession().getAttribute("trdUsrId");
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("user/rmsNewsForUser")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	List response=restTemplate.getForObject(redirectUrl, List.class);
	
	return response;
}

@RequestMapping(value = "/updateStatus",method={RequestMethod.GET,RequestMethod.POST})

public @ResponseBody void updateStatus(HttpServletRequest req,@RequestBody String json) {
	BrokerFormCommand user= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("user/updateStatus")
		    .queryParam("Id",user.getIdStatus())
		    .build()
		    .toUri();
	restTemplate.postForObject(redirectUrl,user,BrokerFormCommand.class);
}

@RequestMapping(value = "/updateStatus1",method={RequestMethod.GET,RequestMethod.POST})

public @ResponseBody List updateStatus1(HttpServletRequest req) {
	
	String userId=(String) req.getSession().getAttribute("trdUsrId");
	BrokerFormCommand user= new BrokerFormCommand();
	user.setUserId(userId);
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("user/updateStatus1")
		    .queryParam("UserId",user.getUserId())
		    .build()
		    .toUri();
	List response=restTemplate.getForObject(redirectUrl, List.class);
	String[] appendArray={"{\"rows\":",response.toString(),"}"};
	return response;
	}


}
	


